package com.xxx.yuedong.course.controller;

import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.xxx.yuedong.common.R;
import com.xxx.yuedong.common.pojo.PageData;
import com.xxx.yuedong.course.controller.form.AddCourseForm;
import com.xxx.yuedong.course.controller.form.SearchCourseListForm;
import com.xxx.yuedong.course.controller.form.UpdateCourseForm;
import com.xxx.yuedong.course.pojo.Course;
import com.xxx.yuedong.course.service.CoursesService;
import com.xxx.yuedong.mall.controller.form.SearchProductListForm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * 课程模块web层
 * 整体架构：
 * web层(controller)->业务层(service)->dao层(MySQL)
 *
 * @Author YinXi
 * @Versin 1.0.0
 * @Date 2023/8/11
 */
@Slf4j
@RestController
@RequestMapping("/course")
@Api(tags = "课程模块")
public class CourseController {
    @Autowired
    private CoursesService coursesService;
    /**
     * 新增课程信息
     */
    @PostMapping("/add-course")
    @ApiOperation("新增课程(后台管理)")
    @ApiOperationSupport(order = 100)
    public R addCourse(@Valid @RequestBody AddCourseForm form){
        System.out.println(form);
        Course course = coursesService.addCourse(form);
        return R.ok().put("course",course);
    }
    /**
     * 更新课程信息
     */
    @PostMapping("/update-course")
    @ApiOperation("更新课程信息(后台管理)")
    @ApiOperationSupport(order = 300)
    public R updateCourse(@Valid @RequestBody UpdateCourseForm form){
        Course course = coursesService.updateCourse(form);
        return R.ok().put("course",course);
    }
    /**
     * 批量删除课程信息
     */
    @PostMapping("/delete-courses")
    @ApiOperation("批量删除课程(后台管理)")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "ids", value = "批量删除的id", required = true, dataType = "long",example = "[1,2]")
    })
    public R deleteCourses(@RequestBody @NotNull Long[] ids){
        for (Object id:ids) {
            if(!(id instanceof Long)){
                return R.error("传入的课程id格式错误");
            }
        }
        coursesService.deleteCourses(ids);
        return R.ok("删除课程信息成功");
    }
    /**
     * 分页查看课程列表
     */
    @GetMapping("")
    @ApiOperationSupport(order = 400)
    @ApiOperation("分页查询课程列表(后台管理)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", paramType = "query", dataType = "long"),
    })
    public R list(@Range(min = 1, message = "请提交有效的页码值！") Integer page) {
        log.debug("开始处理【查询课程列表】的请求，页码：{}", page);
        Integer pageNum = page == null ? 1 : page;
        PageData<Course> pageData = coursesService.list(pageNum);
        return R.ok().put("data",pageData);
    }
    /**
     * 根据课程的id查询课程信息
     */
    @GetMapping("/id/{id:[0-9]+}")
    @ApiOperationSupport(order = 410)
    @ApiOperation("根据课程id查询课程信息(后台管理)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "课程id", dataType = "long",required = true)
    })
    public R getItem(@PathVariable @RequestBody @NotNull @Range(min=1,message = "请提交有效的id") Long id){
        Course course = coursesService.getCourse(id);
        return R.ok().put("data",course);
    }

    /**
     * 根据课程名称中的关键字搜索课程
     */
    @ApiOperation(value = "根据课程名称搜索课程")
    @ApiOperationSupport(order = 420)
    @PostMapping("/search-list")
    public R list(@Valid @RequestBody SearchCourseListForm form) {
        PageInfo pageInfo = coursesService.searchListByName(form.getPageNum(), form.getPageSize(),form.getName());
        return R.ok().put("page",pageInfo);
    }
    /**
     * 查询所有课程
     */
    @ApiOperation(value = "查询所有课程")
    @ApiOperationSupport(order = 430)
    @GetMapping("/get-list")
    public R listCourses() {
        return R.ok().put("courses",coursesService.getListOfCourse());
    }
}
